package com.sin.java.waveaccess;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

public class WaveUtil {

	/**
	 * 获取文件中数据，并转换为字符串
	 * @param filePath
	 * @return
	 */
	public String testData(String filePath) {
		 BufferedReader reader = null;
		 StringBuffer strBuffer = new StringBuffer();
	        try {
	            reader = new BufferedReader(new FileReader(filePath));
	            String tempStr = new String();
	            int line = 1;
	            // 一次读入一行，直到读入null为文件结束
	            while ((tempStr = reader.readLine()) != null) {
	            	strBuffer.append(tempStr);
		            System.out.println("line " + line + ":" + strBuffer);
		            line++;
	            }
	            reader.close();
	        } catch (IOException e) {
	        
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
	        return strBuffer.toString();
		} 
	
	/**
	 * 打印数据
	 * @param temp
	 * @param count
	 * @param cmd
	 * @param index
	 */
	public  void appendCMD(short[] temp,int count, StringBuffer cmd,int index) {
		cmd.append(count + ","); //
		short[] buffer = new short[count];
		System.arraycopy(temp, index-count, buffer, 0, count);
//		System.out.println("cmd1 count:" + count + " index:"+index + " buffer:" + Arrays.toString(buffer));
	}
	
	/**
	 * 打印采样比较大的值的索引
	 * @param filePath
	 * @return
	 */
	public  short[] getShortArrayByFile(String filePath) {
		String data = testData(filePath);
		String[] arrayData = data.split(",");
		short[] temp = new short[arrayData.length];
		for(int i = 0 ; i<arrayData.length;i++){
			temp[i] = Short.parseShort(arrayData[i]);
		}
		return temp;
	}
	
	/**
	 * 打印采样比较大的值的索引
	 * @param filePath
	 * @return
	 */
	public  short[] printIndex(String filePath) {
		String data = testData(filePath);
		System.out.println("data1:"+data);
		String[] arrayData = data.split(",");
		short[] temp = new short[arrayData.length];
		for(int i = 0 ; i<arrayData.length;i++){
			temp[i] = Short.parseShort(arrayData[i]);
			if(Math.abs(temp[i])>3000){
				System.out.println("temp[i]" + i + " value:" + temp[i]);
			}
		}
		return temp;
	}
	
	/**
	 * 获取时序数组
	 */
	public String getWaveCMD(short[] temp,int split , int lowSize){
		int count = 0 ;
		int countTempLow = 0;
		boolean isHigh = false;
		boolean isEnd = false;
		int maxShort = 0 ;
		StringBuffer cmd = new StringBuffer();
		for (int j = 0; j < temp.length; j++) {
			if(maxShort < Math.abs(temp[j])){
				maxShort = Math.abs(temp[j]);
			}
			if (Math.abs(temp[j]) > split) {
				if (!isHigh) {
					if (count > 0) {
						if (!isEnd) {
							appendCMD(temp,count, cmd,j);
						}
					}
					count = 0;
				}
				isHigh = true;
				countTempLow = 0;
				count++;
			} else {
				if (count > 0) {
					countTempLow++;
					if (countTempLow > lowSize) {
						if (isHigh) {
							isHigh = false;
							count = count - countTempLow+1 ;
							if (!isEnd) {
								appendCMD(temp,count, cmd,j-countTempLow+1);
							}
							count = countTempLow;
						} else {
							count++;
							// 低电平大于1000，指令作为结束
							if (count > 1500) {
								isEnd = true;
								appendCMD(temp,count, cmd,j);
								break;
							}
						}
					} else {
						count++;
					}
				}
			}
		}
		System.out.println("getWaveCMD : max:" + maxShort);
		return cmd.toString();
	}
	
	/**
	 * 把44100HZ的数字信号变为38k的数字信号
	 */
	 public String change38HZ(String sample){
		String[] cmds = sample.split(",");
		int[] tempArray = new int[cmds.length];
		StringBuffer buffer = new StringBuffer();
		for(int i =0 ; i < tempArray.length;i++){
			tempArray[i] =  (int) (Integer.parseInt(cmds[i])* (float)38000 /44100) ;
			buffer.append(tempArray[i] + ",");
		}
		return buffer.toString();
	}
	
}
